package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestPermission;
import com.kgc.scd.annotation.RequestUser;
import com.kgc.scd.bean.SignVO;
import com.kgc.scd.bean.User;
import com.kgc.scd.contant.CommonConstant;
import com.kgc.scd.service.UserCreditService;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.UserAnno;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created On : 2022/6/11.
 * <p>
 * Author     : nainai
 * <p>
 * Description: 用户积分操作入口
 */
@RestController
@Slf4j
@Api(tags = "13、用户积分操作入口")
public class UserCreditController {

    @Autowired
    private UserCreditService userCreditService;

    /**
     * @author : nainai
     * @date   : 2022/6/11
     * @param  : [com.kgc.scd.util.UserAnno]
     * @return : com.kgc.scd.util.RequestResult<java.lang.Integer>
     * @description : 查询用户积分
     */
    @PostMapping("/getUserCredit")
    @ApiOperation(value = "查询用户积分", notes = "校验用户是否登录，根据当前登录用户，查询用户积分，支持统一返回")
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS")
    })
    @RequestPermission
    public RequestResult<Integer> getUserCredit(@RequestUser UserAnno user){
        log.info("web-用户：{}，开始执行积分查询", user);
        return ResultBuildUtil.success(userCreditService.queryUserCredit(user));
    }

    /**
     * @author : nainai
     * @date   : 2022/6/11
     * @param  : [com.kgc.scd.util.UserAnno, java.lang.Integer]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 扣减积分
     */
    @PostMapping("/creditDeduct")
    @ApiOperation(value = "扣减积分", notes = "支持统一返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "credit", value = "积分"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    @RequestPermission
    public RequestResult<String> creditDeduct(@RequestUser UserAnno userAnno, @RequestParam Integer credit){
        log.info("web-用户：{}，扣减积分：{}，开始扣减", userAnno, credit);
        return userCreditService.creditDeduct(userAnno.getUserId(), credit) ?
                ResultBuildUtil.success() :
                ResultBuildUtil.fail("积分扣除失败！");
    }

    /**
     * @author : nainai
     * @date   : 2022/6/14
     * @param  : [com.kgc.scd.util.UserAnno, java.lang.Integer]
     * @return : boolean
     * @description : 用户增加积分
     */
    @RequestPermission
    @PostMapping("/sumUserCredit")
    @ApiOperation(value = "增加积分", notes = "支持统一返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "credit", value = "积分"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<String> addUserCredit(@RequestUser UserAnno userAnno, @RequestParam Integer credit){
        log.info("web-用户：{}，增加积分：{}", userAnno, credit);

        // 执行添加操作
        if (userCreditService.addUserCredit(userAnno.getUserId(), credit)) {
            return ResultBuildUtil.success();
        }
        return ResultBuildUtil.fail("增加积分失败！");
    }

    /**
     * @author : nainai
     * @date   : 2022/6/11
     * @param  : [com.kgc.scd.util.UserAnno]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 用户签到
     */
    @RequestPermission
    @PostMapping("/userSign")
    @ApiOperation(value = "用户签到", notes = "支持统一返回")
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<String> consecutiveSign(@RequestUser UserAnno userAnno){
        log.info("web-用户：{}，正在进行签到", userAnno);
        Map<String, String> map = userCreditService.consecutiveSign(userAnno.getUserId());

        return map.get("result").equals("true") ?
                ResultBuildUtil.success(String.format("用户%s签到成功", userAnno.getUserId())) :
                ResultBuildUtil.fail(map.get("result").toString());

    }

    /**
     * @author : nainai
     * @date   : 2022/6/16
     * @param  : [com.kgc.scd.util.UserAnno, java.lang.Integer, java.lang.Integer]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 获取用户签到流水，可以根据年月查询
     */
    @RequestPermission
    @GetMapping("/getUserSignRecord")
    @ApiOperation(value = "用户签到流水", notes = "支持统一返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年"),
            @ApiImplicitParam(name = "month", value = "月"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 503, message = "签到记录为空！")
    })
    public RequestResult<List<SignVO>> getUserSignRecord(@RequestUser UserAnno userAnno, @RequestParam Integer year, @RequestParam(required = false) Integer month) {
        log.info("web-用户：{}，正在查询签到流水，年：{}，月：{}", userAnno, year, month);

        // 查询签到流水
        List<SignVO> signVOList = userCreditService.queryUserSignRecord(userAnno.getUserId(), year, month);

        if (signVOList.isEmpty()) {
            SignVO signVO = SignVO.builder()
                    .userId("签到记录为空")
                    .build();
            return ResultBuildUtil.fail(Arrays.asList(signVO));
        }
        return ResultBuildUtil.success(signVOList);
    }
}
